package com.yc.io.img;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class Img1 {
	
	/**
	 * 通过图片文件头判断图片类型
	 * @param fis
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
    public static String getFileType(String path) throws Exception {
    	String fileType = null;
    	InputStream fis = null;
        try {
        	fis = new FileInputStream(new File(path));
            byte[] bs = new byte[1];
            fis.read(bs);
            String type = Integer.toHexString(bs[0]&0xFF);
            if("ff".equalsIgnoreCase(type)) {
            	fileType = "JPG";
            } else if ("89".equalsIgnoreCase(type)) {
            	fileType = "PNG";
            } else if ("47".equalsIgnoreCase(type)) {
            	fileType = "GIF";
            } else if("42".equalsIgnoreCase(type)) {
            	fileType = "BMP";
            }
        } catch (Exception e) {
        	System.out.println("获取图片类型出错");
            throw new RuntimeException("获取图片类型出错");
        } finally {
        	if (fis != null) {
        		fis.close();
        	}
        }
        System.out.println("fileType=" + fileType);
        return fileType;
    }
    
	public static boolean isImage(File file) {
		boolean flag = false;
		try {
			BufferedImage bufreader = ImageIO.read(file);
			int width = bufreader.getWidth();
			int height = bufreader.getHeight();
			if (width == 0 || height == 0) {
				flag = false;
			} else {
				flag = true;
			}
		} catch (IOException e) {
			flag = false;
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 加载图片
	 */
	public void loadImg() {
		try {
			BufferedImage img = ImageIO.read(new File("E:/git2/jdemo/src/main/resources/images/tankL.gif"));
			
			BufferedImage img2 = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/tankL.gif"));
			
			System.out.println(img);
			System.out.println(img2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		String img1 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\y1 - 副本.jpg";
//		String img2 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\44.jpg";
//		String img3 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\dest2_png.jpg";
//		String img4 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\y7.PNG";
//		String img5 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\y8_png.jpg";
//		String img6 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\g1.gif";
//		String img7 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\g1_gif.png";
//		String img8 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\500463085.bmp";
//		String img9 = "F:\\worksource\\JKCT\\HB\\temp\\testImg\\图片拼接1\\单独测试\\500463085_bmp.jpg";
//		try {
//			getFileType(img1);
//			getFileType(img2);
//			getFileType(img3);
//			getFileType(img4);
//			getFileType(img5);
//			getFileType(img6);
//			getFileType(img7);
//			getFileType(img8);
//			getFileType(img9);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		String img1 = "G:\\000workfile\\v3\\bug\\20200303\\IMG_20200303_161455.jpg";
//		System.out.println(isImage(new File(img1)));
		Img1 ig = new Img1();
		ig.loadImg();

	}

}
